package org.example.entity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Response<T> {
    public int code;   //结果编码(0失败，1成功)
    public String msg;   //描述信息
    public List<T> data;   //数据结果集

    public Response()
    {
        code = 0;
        msg ="";
        data = new ArrayList<T>();
    }
    public Response(int code, String message, List<T> data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }

    public Response<T> success() {
        this.code = 1;
        this.msg ="成功";
        return this;
    }
    public Response<T> fail(String message) {
        this.code = 0;
        this.msg = message;
        return this;
    }
    public Response<T> fail(String message, List<T> data) {
        this.code = 0;
        this.msg = message;
        this.data = data;
        return this;
    }

    public static <T> Response<T> rtnSuccess() {
        return new Response<>(1,"成功", null);
    }
    public static <T> Response<T> rtnSuccess(String msg) {
        return new Response<>(1, msg, null);
    }
    public static <T> Response<T> rtnSuccess(String msg, List<T> data) {
        return new Response<>(1, msg, data);
    }

    public static <T> Response<T> rtnFail(String msg) {
        return new Response<>(0, msg, null);
    }
    public static <T> Response<T> rtnFail(String msg, List<T> data) {
        return new Response<>(0, msg, data);
    }


    public static Response<LinkedHashMap<String, Object>> success(List<LinkedHashMap<String, Object>> data) {
        Response<LinkedHashMap<String, Object>> response = new Response<>();
        response.code = 1;
        response.msg = "成功";
        response.data = data;
        return response;
    }

    public static Response<LinkedHashMap<String, Object>> error() {
        Response<LinkedHashMap<String, Object>> response = new Response<>();
        response.code = 0;
        response.msg = "失败";
        response.data = null;
        return response;
    }

    public static Response<LinkedHashMap<String, Object>> error(String msg) {
        Response<LinkedHashMap<String, Object>> response = new Response<>();
        response.code = 0;
        response.msg = msg != null ? msg : "失败";
        response.data = null;
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
